package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service

public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> getpageResult(EmpQueryParam param) {
        Page<Emp> pages = Page.of(param.getPage(), param.getPageSize());  //获取页码 、 页数
        pages.addOrder(OrderItem.desc("update_time")); //设置排序

        //增加分页搜索的条件
        LambdaQueryWrapper<Emp> querywrapper = Wrappers.<Emp>lambdaQuery()
                .like(param.getName() != null && !"".equals(param.getName()), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());


        pages = page(pages, querywrapper); // 调用 从父类里继承来的page方法完成分页查询




        /* 为每个员工补全部门名称 */
        List<Emp> list = pages.getRecords(); //获取响应数据集合

        Set<Integer> deptIds = list.stream().map(emp -> emp.getDeptId()).collect(Collectors.toSet());  //获取emp表里的deptid值，存入到set集合
        List<Dept> deptList = deptService.listByIds(deptIds); //根据刚刚获取的ids集合，查出ids，每一个的具体值的name是什么

        //将deptlist转换成MaP<部门id，部门名称>
        Map<Integer, String> deptmap = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));  //把查出的name值，转存到map，方便调用id就可以获取name

        //循环每个员工  ，为每个员工的deptname赋值
        for (Emp emp : list) {
            Integer deptId = emp.getDeptId();
            emp.setDeptName(deptmap.get(deptId));
        }


        return new PageResult<>(pages.getTotal(), pages.getRecords());
    }

    /**
     * 新增员工
     * @param emp
     */
//    @Transactional
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveemp(Emp emp) {
        //新增员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);

        //新增工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            //为每个工作经历补充  所属员工的id
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            //再批量保存到emp_expr表里
            empExprService.saveBatch(exprList);

        }
    }
}
